using System;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Text;
using System.Windows.Forms;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ctsu.Data;

namespace ctsu.Admin.BusLogic
{
	public class BLFormReportProtoPiTitle : BLFormReportTimePeriodBaseImpl
	{
		private bool _isNullDates = false;

		public BLFormReportProtoPiTitle()
		{
		}

		protected override bool VerifyDates(out string message)
		{
			message = "";
			if (FormReport.BeginPeriodDate == CremaConstants.NullDate &&
				FormReport.EndPeriodDate == CremaConstants.NullDate)
			{
				_isNullDates = true;
				return true;
			}

			_isNullDates = false;
			if (FormReport.BeginPeriodDate == CremaConstants.NullDate ||
				FormReport.EndPeriodDate == CremaConstants.NullDate)
			{
				message = "Enter both a Begin Date and an End Date or no dates at all.";
				return false;
			}

			if (FormReport.BeginPeriodDate.CompareTo(FormReport.EndPeriodDate) > -1)
			{
				message = "End Date must be equal to or greater than Begin Date";
				return false;
			}
			return true;
		}

		public override bool Run()
		{
			string message = "";
			if (!VerifyDates(out message))
			{
				MessageBox.Show(this.FormReport, message, "Invalid Date", MessageBoxButtons.OK, MessageBoxIcon.Stop);
				return true; ;
			}

			this.FormReport.Grid.DataSource = null;
			this.FormReport.btnPrint.Enabled = false;

			string spName;
			SqlParameter[] aParams = new SqlParameter[2];
			if (this._isNullDates)
				spName = "ProtocolInvestigatorTitleAll";
			else
			{
				spName = "ActiveProtocolInvestigatorTitle";
				aParams[0] = new SqlParameter("@beginDate", FormReport.BeginPeriodDate);
				aParams[1] = new SqlParameter("@endDate", FormReport.EndPeriodDate);
			}

			StringBuilder sb = new StringBuilder();
			//SqlConnection conn = LoginForm.GetSqlConnection();						
			try
			{
				//conn.Open();
				DataSet ds;

				if (this._isNullDates)
					ds = DataAccess.GetDataSet(CommandType.StoredProcedure, spName);
				else
				{
					Database db = DatabaseFactory.CreateDatabase();
					DbCommand cmd = db.GetStoredProcCommand(spName);
					DataAccess.AddParamArrayToDbCommand(cmd, aParams);
					ds = db.ExecuteDataSet(cmd);
				}
				if (ds.Tables.Count > 0)
				{
					ReportData = ds.Tables[0];
					ReportData.TableName = "Report";
				}
				else
					MessageBox.Show(this.FormReport, "No records found for this criteria.", "No Records Found", MessageBoxButtons.OK, MessageBoxIcon.Information);

				if (!IsGridInitialized)
					Initialize();
				this.ReportView = new DataView(this.ReportData);
				this.FormReport.Grid.DataSource = this.ReportView;
				this.FormReport.Count.Text = this.ReportData.Rows.Count.ToString();
				if (this.ReportData.Rows.Count > 0)
					FormReport.btnPrint.Enabled = true;

				return false;

			}
			catch (Exception ex)
			{
				sb.Append("Could not execute stored procedure.");
				sb.Append(Environment.NewLine);
				sb.Append("Error: " + ex.Message);
				throw new Exception(sb.ToString());
			}
			finally
			{
				//if(conn != null)
				//    conn.Dispose();
			}
		}
	}
}
